<?php 
class Usuario_model extends CI_Model {
    private $table = 'tb_usuario';
    
    public function __construct() {
        parent::__construct();
    }

    function update($id, $user) {
        $this->db->where('usu_codigo', $id);
        $this->db->update($this->table, $user);
    }

    function delete($id) {
        $this->db->where('usu_id', $id);
        $this->db->delete($this->table);
    }

    function insert($user) {
        return $this->db->insert($this->table, $user);
    }

    function getAll() {
        $q = $this->db->query("select * from $this->table order by usu_nome");
        $res = $q->result();
        return $res;
    }

    function getById($id) {
        $q = $this->db->where("usu_codigo = $id")->limit(1)->get($this->table)->row();
        return $q;
    }
    
    function validarLogin($login) {
        $q = $this->db->query("select * from $this->table where usu_login = '$login'");
        $res = $q->result();
        if(count($res) == 0){
            return true;
        }
        return false;
    }

    function getByCategoria($categoria) {
        $q = $this->db->query("select * from $this->table, tb_categoria where usu_categoria_id = cat_id and cat_descricao = '$categoria' order by usu_nome");
        $res = $q->result();
        return $res;
    }
}